<?php

/**
 * model_score
 */

class Model_score extends YOU {

	public function __construct()
	{
		parent::__construct();
	}

	/** 读取学科 **/
	public function getSubject($class_id, $nse = 0){
		$data = $this->db->fetchRow("SELECT * FROM `tbl_score_subject` WHERE class_id='{$class_id}'");
		if (!empty($data)){
			if ($nse != 1){
				$data['score_group'] = unserialize($data['score_group']);
				$data['score_order'] = unserialize($data['score_order']);
				$data['score_total'] = unserialize($data['score_total']);
			}
		}
		return $data;
	}

	/** 读取分数段信息 **/
	public function getRangeInfo($range_id){
		$data = $this->db->fetchRow("SELECT * FROM `tbl_score_range` WHERE range_id='{$range_id}'");
		if (!empty($data)){
			$data['range_level']   = unserialize($data['range_level']);
			$data['range_section'] = unserialize($data['range_section']);
		}
		return $data;
	}


	/** 取得分数段学科 **/
	public function getRangeSubject($range_id){
		$data = $this->getRangeInfo($range_id);
		if ($data['subject_id'] != 0){
			$row = $this->db->fetchRow("SELECT subject_name FROM `tbl_subject` WHERE subject_id='{$data['subject_id']}'");
			$name = $row['subject_name'];
		}elseif ($data['score_group'] != ""){
			$name = $row['score_group'];
		}elseif ($data['score_end'] != 0){
			$name = "总分";
		}

		return $name;
	}

	/** 复制给其他班级 **/
	public function copySubject($class_id, $class_lists = array())
	{
		$data = $this->getSubject($class_id, 1);
		if (!empty($class_lists)){
			foreach ($class_lists as $key => $value){
				$info = array(
				"score_group" => $data['score_group'],
				"score_total" => $data['score_total'],
				"score_order" => $data['score_order'],
				);
				$this->setSubject($value, $info);
			}
		}

	}

	/** 写入学科 **/
	public function setSubject($class_id, $info )
	{

		$data = $this->getSubject($class_id, 1);

		// 不存在新增否则修改
		if (empty($data)){
			$info['class_id']    = $class_id;
			$info['status']      = 1;
			$info['create_time'] = date("Y-m-d H:i:s");

			$this->db->do_insert("tbl_score_subject", $info);
			$sc_id = $this->db->insert_id();

		}else{
			$info['update_time'] = date("Y-m-d H:i:s");
			$this->db->do_update("tbl_score_subject", $info, "sc_id='{$data['sc_id']}'");
			$sc_id = $data['sc_id'];
		}

	}


	/** 读取列表 **/
	public function getAll($field = "*")
	{
		$score_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_score` ");

		return $score_lists;
	}

	/** 得到数据 **/
	public function getInfo($score_id, $field = "*")
	{
		$score_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_score` WHERE score_id='{$score_id}'");

		return $score_data;
	}

	/** 记录分数 **/
	public function record($exam_id, $class_id, $student_id, $subject_id, $score)
	{
		$data = $this->getScore($exam_id, $class_id, $student_id, $subject_id);
		if (!empty($data)){
			$info = array(
			"score_value" => $score,
			"update_time" => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_score", $info, "score_id='{$data['score_id']}'");

		}else{
			$info = array(
			"exam_id"     => $exam_id,
			"class_id"    => $class_id,
			"student_id"  => $student_id,
			"subject_id"  => $subject_id,
			"score_value" => $score,
			"exam_id"     => $exam_id,
			"status"      => 1,
			"create_time" => date("Y-m-d H:i:s"),
			);
			$this->db->do_insert("tbl_score", $info);
		}


	}

	/** 取得单次分数 **/
	public function getScore($exam_id, $class_id, $student_id, $subject_id)
	{
		$data = $this->db->fetchRow("SELECT * FROM `tbl_score` WHERE exam_id='{$exam_id}' AND class_id='{$class_id}' AND student_id='{$student_id}' AND subject_id='{$subject_id}' ");

		return $data;

	}

}



?>